package service

import (
	"dks-goweb/model"
	"dks-goweb/utils"
	"encoding/json"
	"strings"
)

func MYService(card string) (model.MYTransformedResponse, error) {

	url := "https://papi.musmoon.com/h5/getCardInfo"
	request, err := utils.GetRequest(url, map[string]string{
		"cardValue": card,
	})

	if err != nil {
		return model.MYTransformedResponse{}, err
	}
	my := model.MYCardResponse{}
	err = json.Unmarshal(request, &my)
	if err != nil {
		return model.MYTransformedResponse{}, err
	}

	// 转换数据
	transformedResponse := model.MYTransformedResponse{
		Content:          my.Content,
		TotalFlow:        my.Object.TotalFlow,
		PackageIntroduce: my.Object.PackageIntroduce,
		Operator:         my.Object.Operator,
		ActiveDate:       my.Object.ActiveDate,
		UsedFlow:         my.Object.UsedFlow,
		PackageName:      my.Object.PackageName,
		ExpirationDate:   my.Object.ExpirationDate,
		OpenDate:         my.Object.OpenDate,
		Status:           my.Object.Status,
	}

	if strings.Contains(my.Content, "未查到卡信息") {
		transformedResponse.CardDealer = "未知"
	} else if strings.Contains(my.Content, "操作成功") {
		transformedResponse.CardDealer = "妙月"
	}

	return transformedResponse, nil
}
